home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / SpellCompositor / SpellBrowserFrame.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  11.6 KB  |  422 lines

  1. /*
  2.     ArcanaApp.SpellBrowserFrame
  3.         - a means by which to browse the contents of a spellbook by level.
  4.     
  5.     Written by Scott C. Ziegler for use with Simulcra™.
  6.     
  7.     vers. history:
  8.         05.31.00 - Added code for button events.  Removed quitting routines from window
  9.                     closing code, meaning that I must add at least a quit action to the
  10.                     menubar.
  11.         09.15.00 - reached a syntactically correct version.
  12.         09.19.00 - decided to overhaul project using layout managers.
  13.         10.25.00 - ***overhaul: began project to make a functional spell book app
  14.         11.30.00 - achieved a compliable state with dummy puppet tester program
  15.     
  16.     Work Needed:
  17.         - 11.11.00 - move selectSpellLevel out to ArcanaApp
  18.         - 11.11.00 - abstract list assignment into a public method with parameters from app.
  19.         - add JDialog warnings to Delete button and other destructive updates.
  20.         - make default display initializer (for constructor and defaults)
  21.     
  22.     Work Completed:
  23.         - added beginning event code for buttons
  24.         - added DefaultListModels to JLists in initLists() function
  25.         - implemented selectSpellLevel(SpellRegisterVector vec)
  26.         - set selection in model to level 1 in initLists()
  27.         - completed event code for buttons
  28.         - populated the menubar with some menuitems linked to events.
  29.  
  30. */
  31.  
  32. /*
  33. This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications.  Any use of this software for any purpose whatsoever must have explicit permission from the author of the file.  This code is part of an ongoing development process for future possible products, and so is considered private property.
  34. Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  35. */
  36.  
  37. package Thoth;
  38.  
  39. import java.awt.*;
  40. import java.awt.event.*;
  41. import java.util.*;
  42.  
  43. import javax.swing.*;
  44. import javax.swing.border.*;
  45. import javax.swing.event.*;
  46.  
  47. public class SpellBrowserFrame extends JFrame {
  48.     
  49.     // Data Members:
  50.  
  51.     private int                 currentSpellsListLevel = 1;
  52.     private DefaultListModel     spellsModel;
  53.     private DefaultListModel     levelModel;
  54.     
  55.     // ActionListeners
  56.     
  57.     ActionListener        editListener;
  58.     ActionListener        viewListener;
  59.     ActionListener        deleteListener;
  60.     ActionListener        aboutListener;
  61.     ActionListener        loadListener;
  62.     ActionListener        saveListener;
  63.     ActionListener        saveAsListener;
  64.     ActionListener        quitListener;
  65.     ActionListener        fwdListener;
  66.     ActionListener        backListener;
  67.     
  68.     ListSelectionListener    levelSelectionListener;
  69.     ListSelectionListener    spellsSelectionListener;
  70.     MouseAdapter        mouseAdapter;
  71.     
  72.     // Menus
  73.     JMenuBar            menuBar;
  74.     JMenu                fileMenu;
  75.         JMenuItem            aboutBoxMenuItem;
  76.         JMenuItem            loadMenuItem;
  77.         JMenuItem            saveMenuItem;
  78.         JMenuItem            saveAsMenuItem;
  79.         JMenuItem            quitMenuItem;
  80.     JMenu                editMenu;
  81.         JMenuItem            undoMenuItem;
  82.         JMenuItem            cutMenuItem;
  83.         JMenuItem            copyMenuItem;
  84.         JMenuItem            pasteMenuItem;
  85.     JMenu                optionsMenu;
  86.         JMenuItem             optionsMenuItem;
  87.     JMenu                someMenu;
  88.         JMenuItem            someMenuItem;
  89.     
  90.     // Buttons
  91.     JButton        backButton;
  92.     JButton        fwdButton;
  93.     JButton        viewButton;
  94.     JButton        editButton;
  95.     JButton        deleteButton;
  96.     
  97.     // Panels
  98.     JPanel        buttonPanel;
  99.     
  100.     // SplitPane
  101.     JSplitPane    splitPane;
  102.     
  103.     JScrollPane    levelScrollPane;
  104.     JScrollPane    spellsScrollPane;
  105.     JList        levelList;
  106.     JList        spellsList;
  107.     
  108.     // Constructors
  109.     
  110.     public SpellBrowserFrame() {
  111.         super("Arcana Spell Browser");
  112.         
  113.         initMenus();
  114.         initButtons();
  115.         initLists();
  116.         initBrowserPane();
  117.         
  118.         setJMenuBar(menuBar);
  119.         Container progFrameContentPane = getContentPane();
  120.         
  121.         progFrameContentPane.add(splitPane, BorderLayout.CENTER);
  122.         progFrameContentPane.add(buttonPanel, BorderLayout.SOUTH);
  123.         
  124.         setSize(new Dimension(640,480));
  125.         //setVisible(true);
  126.         
  127.         }
  128.     
  129.     // Methods
  130.     
  131.     // Selectors
  132.     
  133.     public int getCurrentSpellsListLevel() { return currentSpellsListLevel; }
  134.     
  135.     public JList getSpellsList() { return spellsList; }
  136.     
  137.     public JList getLevelList() { return levelList; }
  138.     
  139.     // Mutators
  140.     
  141.     public void dimSave() { saveMenuItem.setEnabled(false); }
  142.     
  143.     public void undimSave() { saveMenuItem.setEnabled(true); }
  144.     
  145.     public void setSpellsListModel(DefaultListModel dlm) { spellsModel = dlm; }
  146.         
  147.     public void setLevelListModel(DefaultListModel dlm) { levelModel = dlm; }
  148.         
  149.     public void setViewListener(ActionListener vl) { 
  150.         viewButton.removeActionListener(viewListener);
  151.         viewButton.addActionListener(vl);
  152.         }
  153.     
  154.     public void setEditListener(ActionListener el) { 
  155.         editButton.removeActionListener(editListener);
  156.         editButton.addActionListener(el);
  157.         }
  158.         
  159.     public void setDeleteListener(ActionListener dl) { 
  160.         deleteButton.removeActionListener(deleteListener);
  161.         deleteButton.addActionListener(dl);
  162.         }
  163.         
  164.     public void setAboutListener(ActionListener al) {
  165.         aboutBoxMenuItem.removeActionListener(aboutListener);
  166.         aboutBoxMenuItem.addActionListener(al);
  167.         }
  168.         
  169.     public void setLoadListener(ActionListener ll) {
  170.         loadMenuItem.removeActionListener(loadListener);
  171.         loadMenuItem.addActionListener(ll);
  172.         }
  173.     
  174.     public void setSaveListener(ActionListener sl) {
  175.         saveMenuItem.removeActionListener(saveListener);
  176.         saveMenuItem.addActionListener(sl);
  177.         }
  178.         
  179.     public void setSaveAsListener(ActionListener sal) {
  180.         saveAsMenuItem.removeActionListener(saveAsListener);
  181.         saveAsMenuItem.addActionListener(sal);
  182.         }    
  183.  
  184.     public void setQuitListener(ActionListener ql) { 
  185.         quitMenuItem.removeActionListener(quitListener);
  186.         quitMenuItem.addActionListener(ql);
  187.         }
  188.     
  189.     public void setFwdListener(ActionListener fl) { 
  190.         fwdButton.removeActionListener(fwdListener);
  191.         fwdButton.addActionListener(fl);
  192.         }
  193.     
  194.     public void setBackListener(ActionListener bl) { 
  195.         backButton.removeActionListener(backListener);
  196.         backButton.addActionListener(bl);
  197.         }
  198.     
  199.     public void setLevelListSelectionListener(ListSelectionListener lsl) {
  200.         levelList.removeListSelectionListener(levelSelectionListener);
  201.         levelList.addListSelectionListener(lsl);
  202.         }
  203.         
  204.     public void setSpellsListSelectionListener(ListSelectionListener lsl) {
  205.         spellsList.removeListSelectionListener(spellsSelectionListener);
  206.         spellsList.addListSelectionListener(lsl);
  207.         }
  208.         
  209.     public void setSpellsListMouseListener(MouseListener ml) {
  210.         spellsList.removeMouseListener(mouseAdapter);
  211.         spellsList.addMouseListener(ml);
  212.         }
  213.     
  214.     // initMenus AOK
  215.     private void initMenus() {
  216.         menuBar = new JMenuBar();
  217.         menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
  218.  
  219.         fileMenu = new JMenu("File");
  220.         fileMenu.setMnemonic('F');
  221.         //
  222.         aboutBoxMenuItem = new JMenuItem("About…");
  223.         fileMenu.add(aboutBoxMenuItem);
  224.         aboutListener = new ActionListener() {
  225.             public void actionPerformed(ActionEvent ae) {
  226.                 System.out.println("About!");
  227.                 }
  228.             };
  229.         aboutBoxMenuItem.addActionListener(aboutListener);
  230.         //
  231.         loadMenuItem = new JMenuItem("Load…");
  232.         fileMenu.add(loadMenuItem);
  233.         loadListener = new ActionListener() {
  234.             public void actionPerformed(ActionEvent ae) {
  235.                 System.out.println("load!");
  236.                 }
  237.             };
  238.         loadMenuItem.addActionListener(loadListener);
  239.         //
  240.         saveMenuItem = new JMenuItem("Save");
  241.         fileMenu.add(saveMenuItem);
  242.         saveListener = new ActionListener() {
  243.             public void actionPerformed(ActionEvent ae) {
  244.                 System.out.println("save!");
  245.                 }
  246.             };
  247.         saveMenuItem.addActionListener(saveListener);
  248.         //
  249.         saveAsMenuItem = new JMenuItem("Save As…");
  250.         fileMenu.add(saveAsMenuItem);
  251.         saveAsListener = new ActionListener() {
  252.             public void actionPerformed(ActionEvent ae) {
  253.                 System.out.println("SaveAs!");
  254.                 }
  255.             };
  256.         saveAsMenuItem.addActionListener(saveAsListener);
  257.         //
  258.         quitMenuItem = new JMenuItem("Quit");
  259.         fileMenu.add(quitMenuItem);
  260.         quitListener = new ActionListener() {
  261.             public void actionPerformed(ActionEvent ae) {
  262.                 System.out.println("Quit!");
  263.                 }
  264.             };
  265.         quitMenuItem.addActionListener(quitListener);
  266.         //
  267.         editMenu = new JMenu("Edit");
  268.         editMenu.setMnemonic('E');
  269.         //
  270.         undoMenuItem = new JMenuItem("Undo");
  271.         editMenu.add(undoMenuItem);
  272.         //
  273.         cutMenuItem = new JMenuItem("Cut");
  274.         editMenu.add(cutMenuItem);
  275.         //
  276.         copyMenuItem = new JMenuItem("Copy");
  277.         editMenu.add(copyMenuItem);
  278.         //
  279.         pasteMenuItem = new JMenuItem("Paste");
  280.         editMenu.add(pasteMenuItem);
  281.         //
  282.         optionsMenu = new JMenu("Options");
  283.         optionsMenuItem = new JMenuItem("Options…");
  284.         optionsMenu.add(optionsMenuItem);
  285.         //
  286.         someMenu = new JMenu("Something");
  287.         //
  288.         someMenuItem = new JMenuItem("Something…");
  289.         //
  290.         menuBar.add(fileMenu);
  291.         menuBar.add(editMenu);
  292.         menuBar.add(optionsMenu);
  293.         menuBar.add(someMenu);
  294.         }
  295.         
  296.     private void initButtons() {
  297.         backButton = new JButton("Back");
  298.         backListener = new ActionListener() {
  299.             public void actionPerformed(ActionEvent ae) {
  300.                 System.out.println("Back!");
  301.                 }
  302.             };
  303.         backButton.addActionListener(backListener);
  304.         
  305.         fwdButton = new JButton("Fwd");
  306.         fwdListener = new ActionListener() {
  307.             public void actionPerformed(ActionEvent ae) {
  308.                 System.out.println("Fwd!");
  309.                 }
  310.             };
  311.         fwdButton.addActionListener(fwdListener);
  312.         
  313.         viewButton = new JButton("View");
  314.         viewListener = new ActionListener() {
  315.             public void actionPerformed(ActionEvent ae) {
  316.                 System.out.println("View!");
  317.                 }
  318.             };
  319.         viewButton.addActionListener(viewListener);
  320.         
  321.         editButton = new JButton("Edit");
  322.         editListener = new ActionListener() {
  323.             public void actionPerformed(ActionEvent ae) {
  324.                 System.out.println("Edit!");
  325.                 }
  326.             };
  327.         editButton.addActionListener(editListener);
  328.         
  329.         deleteButton = new JButton("Delete");
  330.         deleteListener = new ActionListener() {
  331.             public void actionPerformed(ActionEvent ae) {
  332.                 System.out.println("Delete!");
  333.                 }
  334.             };
  335.         deleteButton.addActionListener(deleteListener);
  336.         
  337.         buttonPanel = new JPanel();
  338.         buttonPanel.setLayout(new GridLayout(1,5,5,5));
  339.         buttonPanel.add(backButton);
  340.         buttonPanel.add(fwdButton);
  341.         buttonPanel.add(editButton);
  342.         buttonPanel.add(viewButton);
  343.         buttonPanel.add(deleteButton);
  344.         
  345.         }
  346.     
  347.     // initLists AOK
  348.     private void initLists() {
  349.         spellsModel = new DefaultListModel();
  350.         levelModel = new DefaultListModel();
  351.         levelList = new JList();
  352.         spellsList = new JList();
  353.         spellsList.setModel(spellsModel);
  354.         levelList.setModel(levelModel);
  355.         levelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  356.         spellsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  357.         
  358.         
  359.         Vector vec = new Vector(9);
  360.         vec.addElement("Level 1");
  361.         vec.addElement("Level 2");
  362.         vec.addElement("Level 3");
  363.         vec.addElement("Level 4");
  364.         vec.addElement("Level 5");
  365.         vec.addElement("Level 6");
  366.         vec.addElement("Level 7");
  367.         vec.addElement("Level 8");
  368.         vec.addElement("Level 9");
  369.         
  370.         levelList.setListData(vec);
  371.         levelList.setSelectionInterval(0,0);
  372.         
  373.         levelSelectionListener = new ListSelectionListener() {
  374.             public void valueChanged(ListSelectionEvent lse) {
  375.                 System.out.println(lse.toString());
  376.                 }
  377.             };
  378.         levelList.addListSelectionListener(levelSelectionListener);
  379.         
  380.         spellsSelectionListener = new ListSelectionListener() {
  381.             public void valueChanged(ListSelectionEvent lse) {
  382.                 System.out.println(lse.toString());
  383.                 }
  384.             };
  385.         spellsList.addListSelectionListener(spellsSelectionListener);
  386.         
  387.         mouseAdapter = new MouseAdapter() {
  388.             public void mouseClicked(MouseEvent me) {
  389.                 
  390.                 }
  391.             };
  392.         spellsList.addMouseListener(mouseAdapter);
  393.         }
  394.     
  395.     // initBrowserPane AOK
  396.     private void initBrowserPane() {
  397.         levelScrollPane = new JScrollPane();
  398.         spellsScrollPane = new JScrollPane();
  399.         
  400.         levelScrollPane.getViewport().add(levelList);
  401.         spellsScrollPane.getViewport().add(spellsList);
  402.         
  403.         splitPane = new JSplitPane();
  404.         splitPane.add(levelScrollPane, JSplitPane.LEFT);
  405.         splitPane.add(spellsScrollPane, JSplitPane.RIGHT);
  406.         }
  407.         
  408.     public void clearSpellsModel() {
  409.         spellsList.setListData(new Vector());
  410.         }
  411.         
  412.     public void changeSpellsModel(Vector spellsVector) {
  413.         spellsList.setListData(spellsVector);
  414.         }
  415.         
  416.     void thisWindowClosing(java.awt.event.WindowEvent e) {
  417.         setVisible(false);
  418.         // dispose(); keep the memory of the frame to re-init!!!
  419.         // System.exit(0); this quits the app?!?!
  420.         }
  421.         
  422.     }